#!/usr/bin/env python

#        +-----------------------------------------------------------------------------+
#        | GPL                                                                         |
#        +-----------------------------------------------------------------------------+
#        | Copyright (c) Mattia Lambertini <lamberti@cs.unibo.it>                      |
#        |                                                                             |
#        | This program is free software; you can redistribute it and/or               |
#        | modify it under the terms of the GNU General Public License                 |
#        | as published by the Free Software Foundation; either version 2              |
#        | of the License, or (at your option) any later version.                      |
#        |                                                                             |
#        | This program is distributed in the hope that it will be useful,             |
#        | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
#        | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
#        | GNU General Public License for more details.                                |
#        |                                                                             |
#        | You should have received a copy of the GNU General Public License           |
#        | along with this program; if not, write to the Free Software                 |
#        | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
#        +-----------------------------------------------------------------------------+

try:
    import pygtk
    # tell pyGTK, if possible, that we want GTKv2
    pygtk.require("2.0")
except:
    # Some distributions come with GTK2, but not pyGTK
    pass

try:
    import gtk
except:
    print "You need to install pyGTK or GTKv2 ",
    print "or set your PYTHONPATH correctly."
    print "try: export PYTHONPATH=",
    print "/usr/local/lib/python2.2/site-packages/"
    sys.exit(1)


import sys
import gnomeapplet

from postit_list import PostitList
from postit import Postit


__author__="Mattia Lambertini"
__date__ ="$Aug 22, 2009 8:36:02 PM$"

__noteList = []
__postitList = None

FLAG = True

def factory(applet, iid):
    button = gtk.Button()
    button.set_relief(gtk.RELIEF_NONE)
    # check if exist the icon in the theme
    # if not exist set some Text in the button label
    icon = __load_icon()
    if icon == None:
        button.set_label("LV")
    else:
        # otherwise set the icon
        button.set_image(icon)
    button.connect("button_press_event", show_menu, applet)
    applet.add(button)
    applet.show_all()
    return True

def __load_icon():
    """ Return the default icon (gtk.Image)"""
    theme = gtk.icon_theme_get_default()
    icon = gtk.Image()
    iconName = "lovenote"
    if iconName in theme.list_icons():
        pixbufIcon = theme.load_icon(iconName, 18, gtk.ICON_LOOKUP_FORCE_SVG)
        icon.set_from_pixbuf(pixbufIcon)
    else:
        print "No icon found!"
        icon = None
        
    return icon
        
def show_menu(widget, event, applet):
    if event.type == gtk.gdk.BUTTON_PRESS and event.button == 3:
        widget.emit_stop_by_name("button_press_event")
        create_right_menu(applet)
    elif event.type == gtk.gdk.BUTTON_PRESS and event.button == 1:
        widget.emit_stop_by_name("button_press_event")
        global __postit_list
        global FLAG
        global __postitList
        # if is the first time i need to read the list of notes
        if FLAG == True:
            __postitList = PostitList()
           # __postit_list = __postitList.postit_list
            FLAG = False
        else:
            visibility = __postitList.postit_list[0].window.get_property("visible")
            for postit in __postitList.postit_list:
                postit.set_status(not visibility)

def create_right_menu(applet):
    propxml="""
            <popup name="button3">
            <menuitem name="Item 1" verb="nnote" _label="new post-it"
                pixtype="stock" pixname="lovenote"/>
            <menuitem name="Item 2" verb="Prefs" _label="_Preferences"
                pixtype="stock" pixname="gtk-properties"/>
            <menuitem name="Item 3" verb="Help" _label="_Help" pixtype="stock"
                pixname="gtk-help"/>
            <menuitem name="Item 4" verb="About" label="_About" pixtype="stock"
                pixname="gtk-about"/>
            </popup>"""
    verbs = [("About", show_about_dialog), ("nnote", create_new_note)]
    applet.setup_menu(propxml, verbs, None)

def show_about_dialog(*arguments, **keywords):
    dialog = gtk.AboutDialog()
    dialog.connect("response", lambda d, r: d.destroy())
    authors = 'Mattia Lambertini <lamberti@cs.unibo.it>',
    dialog.set_authors(authors)
    dialog.run()

def create_new_note(*arguments, **keywords):
    """"""

    __postitList.add_postit()


# for debugging
if len(sys.argv) == 2:
    if sys.argv[1] == "-riw":
        mainWindow = gtk.Window(gtk.WINDOW_TOPLEVEL)
        mainWindow.set_title("Love Note Applet")
        mainWindow.connect("destroy", gtk.main_quit)
        applet = gnomeapplet.Applet()
        factory(applet, None)
        applet.reparent(mainWindow)
        mainWindow.show_all()
        gtk.main()
        sys.exit()
    else:
        print "lovenote.py -riw"


if __name__ == '__main__':
    print "Starting factory"
    gnomeapplet.bonobo_factory("OAFIID:Gnome_Panel_LoveNote_Factory", \
                            gnomeapplet.Applet.__gtype__, \
                            "loveNote", "1.0", factory)
